package com.jsti.ccp.common.result;

import lombok.Data;

import java.io.Serializable;

/**
 * http接口返回结果
 * @param <T>
 */
@Data
public class HttpResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 服务异常编码
     */
    private static final Integer ERROR = 500;

    /**
     * 服务正常编码
     */
    private static final Integer OK = 200;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    private T result;


    /**
     * 时间戳
     */

    private long timestamp = System.currentTimeMillis();


    public HttpResult() {

    }


    public HttpResult<T> success(String message) {
        this.message = message;
        this.code = OK;
        this.success = true;
        return this;
    }


    public static HttpResult<Object> ok() {
        HttpResult<Object> r = new HttpResult<Object>();
        r.setSuccess(true);
        r.setCode(OK);
        r.setMessage("成功");
        return r;
    }

    public static HttpResult<Object> ok(String msg) {
        HttpResult<Object> r = new HttpResult<Object>();
        r.setSuccess(true);
        r.setCode(OK);
        r.setMessage(msg);
        return r;
    }

    public static HttpResult<Object> ok(Object data) {
        HttpResult<Object> r = new HttpResult<Object>();
        r.setSuccess(true);
        r.setCode(OK);
        r.setResult(data);
        return r;
    }

    public static HttpResult<Object> error(String msg) {
        return error(ERROR, msg);
    }

    public static HttpResult<Object> error(int code, String msg) {
        HttpResult<Object> r = new HttpResult<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static HttpResult<Object> error(int code, String msg, String exception) {
        HttpResult<Object> r = new HttpResult<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        r.setResult(exception);
        return r;
    }

}
